package com.example.exam18;

import android.os.AsyncTask;
import android.os.Message;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

class MyThread extends AsyncTask {
    String str;
    MainActivity activity;
    public MyThread(String str,MainActivity activity){
        this.str=str;
        this.activity=activity;
    }

    @Override
    protected Object doInBackground(Object[] objects) {
        try {
            URL url = new URL(str);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String string=null;
            while((string=br.readLine())!=null){
                stringBuffer.append(string);
            }
            String s=stringBuffer.toString();
            Message obtain = Message.obtain();
            obtain.obj=s;
            obtain.what=100;
            activity.handler.sendMessage(obtain);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
